﻿using System.Collections.Generic;
using DBConnection.Helper;
using Devart.Data.PostgreSql;

namespace DBConnection.ConnectionClass
{
    internal class PostgreSqlDataBaseConnection : BaseConnection
    {

        private PgSqlConnection _aConnection;
        private PgSqlCommand _aCommand;
        private PgSqlDataReader _aReader;

        public PostgreSqlDataBaseConnection()
            : base()
        {
            _aConnection = new PgSqlConnection();
        }

        public PostgreSqlDataBaseConnection(string aConnectionstring)
            : base(aConnectionstring)
        {
            _aConnection = new PgSqlConnection(aConnectionstring);
        }

        public override List<string>[] Select(Table tablename)
        {
            _aConnection = new PgSqlConnection(ConnectionString);
            _aCommand = _aConnection.CreateCommand();
            _aCommand.CommandText = "select * from " + tablename.Name;
            _aConnection.Open();
            _aReader = _aCommand.ExecuteReader();
            res = new List<string>[_aReader.FieldCount];
            for (int i = 0; i < _aReader.FieldCount; i++)
            {
                res[i] = new List<string>();
            }
            int j = 0;
            while (_aReader != null && _aReader.Read())
            {
                j++;
                for (int i = 0; i < _aReader.FieldCount; i++)
                {
                    res[j].Add(_aReader[i].ToString());
                }
            }
            return res;
        }
        public override List<string>[] Select(Table tablename, params Column[] column)
        {
            _aConnection = new PgSqlConnection(ConnectionString);
            _aCommand = _aConnection.CreateCommand();
            string command = "select ";
            for (int i = 0; i < column.Length - 1; i++)
            {
                command += column[i].Name + ",";
            }
            command += column[column.Length - 1].Name + " from " + tablename.Name;
            _aCommand.CommandText = command;
            _aConnection.Open();
            _aReader = _aCommand.ExecuteReader();
            res = new List<string>[_aReader.FieldCount];
            for (int i = 0; i < _aReader.FieldCount; i++)
            {
                res[i] = new List<string>();
            }
            int j = 0;
            while (_aReader != null && _aReader.Read())
            {
                j++;
                for (int i = 0; i < _aReader.FieldCount; i++)
                {
                    res[j].Add(_aReader[i].ToString());
                }
            }
            return res;
        }
    }
}
